<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Like extends Model
{
    protected $table = "c_likes";

    /****
     * @param $filter
     * @return int
     * 点赞或取消
     */
    public function setWorkFollow($filter)
    {
        $info = $this->getInfos($filter);
        if (!empty($info)) { //取消收藏
            $res = $this->where("article_id", '=', $filter['article_id'])
                ->where("user_id", '=', $filter['user_id'])
                ->where("work_id", '=', $filter['work_id'])
                ->where('type', '=', $filter['type'])->delete();
            if ($res) {
                return 1;
            } else {
                return 2;
            }
        } else {
            $this->article_id = $filter['article_id'];
            $this->user_id = $filter['user_id'];
            $this->type = $filter['type'];
            $this->work_id = $filter['work_id'];
            $res = $this->save();
            if ($res) {
                return 3;
            } else {
                return 4;
            }
        }
    }

    //详情
    public function getInfos($data)
    {
        return $this->where("article_id", '=', $data['article_id'])
            ->where("user_id", '=', $data['user_id'])
            ->where('type', '=', $data['type'])
            ->first();
    }
}
